package com.app.util.common;


import java.text.SimpleDateFormat;

import java.util.Date;





public class Format {

	
	
	
	/**
	 * 截取name 
	 * @param value
	 * @return
	 */
	public static String subName(String value){
		String result = "";
		if(value == null || value.equals("")){
			return "";
		}
		String[] arr = value.split(",");
		if(arr != null){
			for(int i=0;i<arr.length;i++){
				String temp = arr[i];
				String[] arr_temp = temp.split("/");
				if(arr_temp != null && arr_temp.length == 1){
					result += arr_temp[0];
				}else if(arr_temp != null && arr_temp.length == 2){
					result += arr_temp[1];
				}else if(arr_temp != null && arr_temp.length == 3){
					result += arr_temp[2];	//
				}else if(arr_temp != null && arr_temp.length == 4){
					result += arr_temp[2];	//
				}
				
				if(i != arr.length -1){
					result += ",";
				}
			}
		}
		
		return result;
	}
	
	/**
	 * 截取name 和mobile
	 * 
	 * @param value
	 * @return
	 */
	public static String subNameAndMobile(String value){
		String result = "";
		if(value == null || value.equals("")){
			return "";
		}
		String[] arr = value.split(",");
		if(arr != null){
			for(int i=0;i<arr.length;i++){
				String temp = arr[i];
				String[] arr_temp = temp.split("/");
				if(arr_temp != null && arr_temp.length == 1){
					result += arr_temp[0];
				}else if(arr_temp != null && arr_temp.length == 2){
					result += arr_temp[1];
				}else if(arr_temp != null && arr_temp.length == 3){
					result += arr_temp[2];	//
				}else if(arr_temp != null && arr_temp.length == 4){
					result += arr_temp[2] + "/" + arr_temp[3];	//
				}
				
				if(i != arr.length -1){
					result += ",";
				}
			}
		}
		
		return result;
	}
	/**
	 * 截取id 
	 * @param value
	 * @return
	 */
	public static String subId(String value){
		String result = "";
		if(value == null || value.equals("")){
			return "";
		}
		String[] arr = value.split(",");
		if(arr != null){
			for(int i=0;i<arr.length;i++){
				String temp = arr[i];
				String[] arr_temp = temp.split("/");
				if(arr_temp != null && arr_temp.length == 1){
					result += arr_temp[0];
				}else if(arr_temp != null && arr_temp.length >= 2){
					result += arr_temp[1];
				}
				if(i != arr.length -1){
					result += ",";
				}
			}
		}
		
		return result;
	}
	
	/**
	 * 截取mobile
	 * @param value
	 * @return
	 */
	public static String subMobile(String value){
		String result = "";
		if(value == null || value.equals("")){
			return "";
		}
		String[] arr = value.split(",");
		if(arr != null){
			for(int i=0;i<arr.length;i++){
				String temp = arr[i];
				String[] arr_temp = temp.split("/");
				if(arr_temp != null){
					result += arr_temp[arr_temp.length-1];
				}
				
				if(i != arr.length -1){
					result += ",";
				}
			}
		}
		
		return result;
	}
	
	
	/**
	 * 日期比较相差天数
	 * @param beginDate
	 * @param endDate
	 * @return
	 */
	public static Long diffDay(String beginDate,String endDate){
		Long diffDay = 0L;
		
		try{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			if(beginDate == null || beginDate.equals("")){
				beginDate = sdf.format(new Date());
			}
			if(endDate == null || endDate.equals("")){
				endDate = sdf.format(new Date());
			}
			
	        Date begin = sdf.parse(beginDate);
	        Date end = sdf.parse(endDate);
	        diffDay = (end.getTime()-begin.getTime())/(1000*3600*24);
	       
		}catch(Exception e){
			e.printStackTrace();
		}
		return diffDay;
	}
	
	

	/**
	 * 将字符串转换成Date类型，自动匹配
	 * @param value
	 * @return
	 */
	public static Date getDate(String dateString ) {
		
		// TODO Auto-generated method stub
		
		if(dateString == null || dateString.equals("")) {
			return null;
		}
		try {
			return new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
		}catch (Exception e) {
		}
		try {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(dateString);
		}catch (Exception e) {
		}
		try {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateString);
		}catch (Exception e) {
		}
		try {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").parse(dateString);
		}catch (Exception e) {
		}
		
		try {
			return new SimpleDateFormat("yyyy年MM月dd日").parse(dateString);
		}catch (Exception e) {
		}
		
		try {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateString);
		}catch (Exception e) {
		}
		
		try {
			return new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").parse(dateString);
		}catch (Exception e) {
		}
		
		try {
			return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse(dateString);
		}catch (Exception e) {
		}
		
		
		
		try {
			return new SimpleDateFormat("yyyy/MM/dd").parse(dateString);
		}catch (Exception e) {
		}
		return null;
	}
	
	
	/**
	 * 过滤html
	 * @param s
	 * @return
	 */
	 public static String toTxt(String s) {
        if (s == null) {
            return null;
        } else {
        	s=s.replaceAll("<P>","");
        	s=s.replaceAll("<p>","");
        	s=s.replaceAll("<BR>","");
        	s=s.replaceAll("<br>","");
        	s=s.replaceAll("</P>","");
        	s=s.replaceAll("</p>","");
        	s=s.replaceAll("<[.[^<]]*>","");
        	s=s.replaceAll("《P》","");
            s=s.replaceAll("\n", "");
            s=s.replaceAll("\t", "");
            s=s.replaceAll(" ", "");
            s=s.replaceAll("\r\n", "");
            s=s.replaceAll("&ldquo;", "“");
        	s=s.replaceAll("&rdquo;", "”");
        	s=s.replaceAll("&quot;", "＂");
            s=s.replaceAll("&mdash;", "—");
            s=s.replaceAll("&nbsp;", " ");
            return s;
            
        }
	}
	 
	 public static String toHtml(String s) {
        if (s == null) {
            return null;
        } else {
            String s1 = new String(s);
            //s1 = toHtmlInput(s1);
            //s1 = Replace(s1, "\r\n", "\n");
            s1 = s1.replaceAll("\r\n", "<br/>");
            s1 = s1.replaceAll("\n", "<br/>");
            s1 = s1.replaceAll("\r", "<br/>");
            s1 = s1.replaceAll("\t", "   ");
            s1 = s1.replaceAll(" ", "&nbsp;");
            //s1 = Replace(s1, "\n", "<br>\n");
            //s1 = Replace(s1, "\t", "    ");
            //s1 = Replace(s1, "  ", " &nbsp;");
            return s1;
        }
    }
	 
	 public static String formatString(String s) {
		s = s.replaceAll("，", ",");
		s = s.replaceAll(" ", "\n");
		s = s.replaceAll(" ", "\r");
		s = s.replaceAll("＜", "<");
		s = s.replaceAll("＞", ">");
		s = s.replaceAll("＂", "\"");
		s = s.replaceAll("＇", "'");
		s = s.replaceAll("％", "%");
		return s;
	}

	public static String removeNull(String value) {
		// TODO Auto-generated method stub
		
		if(value == null) {
			return "";
		}
		return value;
	}

	public static String toWorld(String s) {
        if (s == null) {
            return null;
        } else {
            s = s.replaceAll("\r\n", "");
            s = s.replaceAll("\n", "");
            s = s.replaceAll("\"", "\\\"");
            return s;
        }
    }
	
}
